package com.woniu.adapter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.woniu.exception.MaterialReceivedException;
import com.woniu.outlet.client.WorkingProcedures;
import com.woniu.outlet.dao.ApplicationDetailMapper;
import com.woniu.outlet.dao.MaterialApplicationMapper;
import com.woniu.outlet.po.ApplicationDetail;
import com.woniu.outlet.po.MaterialApplication;
import com.woniu.outlet.redis.ERPRedisRepo;
import com.woniu.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class MaterialAppAdapter {
    @Autowired
    private ERPRedisRepo erpRedisRepo;
    @Autowired
    private MaterialApplicationMapper materialApplicationMapper;
    @Autowired
    private ApplicationDetailMapper applicationMapping;
    @Autowired
    private WorkingProcedures userClient;
    @Autowired
    private WorkingProcedures workingProcedures;

//    @Autowired
//    private MatRabbitmqRepo matRabbitmqRepo;
    ObjectMapper mapper = new ObjectMapper();
    CollectionType type = mapper.getTypeFactory().constructCollectionType(List.class, MaterialApplication.class);
    /** 获取所有的清领
     * @return
     */
    public List<MaterialApplication> getMaterialApplicationList(Date t1,Date t2) throws JsonProcessingException {
        String str = erpRedisRepo.getList("materialApplicationList");
        List<MaterialApplication> lists = new ArrayList<>();
        if(StringUtils.isEmpty(str)){
            lists = materialApplicationMapper.selectAll(t1,t2);
            erpRedisRepo.addList("materialApplicationList",mapper.writeValueAsString(lists));
        }else{
            lists = mapper.readValue(str,type);
        }
        workingProcedures.addSystemLog("获取所有的清领");
        //存储到redis中
        return lists;
    }

    public void addMaterialApplication(MaterialApplication materialApplication){
        List<com.woniu.inlet.vo.ApplicationDetail> applicationDetails = materialApplication.getApplicationDetails();
        materialApplication.setSpare1("未领取");
        ResponseResult<Long> workerByCode = userClient.getWorkerByCode(materialApplication.getUsername());
        materialApplication.setUserId(workerByCode.getData().intValue());
        materialApplicationMapper.insertMat(materialApplication);
        List<ApplicationDetail> applicationDetailArrayList = new ArrayList<>();
        for (com.woniu.inlet.vo.ApplicationDetail applicationDetail : applicationDetails) {
            ApplicationDetail applicationDetail1 = new ApplicationDetail();
            BeanUtils.copyProperties(applicationDetail,applicationDetail1);
            applicationDetailArrayList.add(applicationDetail1);
        }

        for (ApplicationDetail applicationDetail : applicationDetailArrayList) {
            applicationDetail.setApplicationId(materialApplication.getId());
            applicationMapping.insertSelective(applicationDetail);
        }
        workingProcedures.addSystemLog("添加物料请领");
        erpRedisRepo.deleteList("materialApplicationList");
//        matRabbitmqRepo.sendAppMessage(materialApplication.getId()+"");
    }

    public void deleteMaterialApplication(Integer id){
        MaterialApplication materialApplication = new MaterialApplication(id);
        materialApplicationMapper.deleteByPrimaryKey(id);
        erpRedisRepo.deleteList("materialApplicationList");
        workingProcedures.addSystemLog("删除物料请领");
    }

    public void updateMaterialApplication(MaterialApplication materialApplication){
        materialApplicationMapper.updateByPrimaryKeySelective(materialApplication);
//        erpRedisRepo.deleteList("materialApplicationList");
        workingProcedures.addSystemLog("修改物料请领");
    }

    public MaterialApplication getMaterialApplicationByCode (String applicationCode) throws JsonProcessingException {

        MaterialApplication materialApplication = materialApplicationMapper.selectApp(applicationCode);
        if(StringUtils.isEmpty(materialApplication)){
            //物料已被领取
            throw new MaterialReceivedException("没有这种物料的申请");
        }
        List<ApplicationDetail> list = new ArrayList<>();
        String str = erpRedisRepo.getList( "applicationDetailList");
        if(StringUtils.isEmpty(str)){
            list = materialApplicationMapper.selectMaterialByCode(applicationCode);
            erpRedisRepo.addList(mapper.writeValueAsString(list), "applicationDetailList");
        }else{
            list = mapper.readValue(str,type);
        }
        List<com.woniu.inlet.vo.ApplicationDetail> applicationDetails = new ArrayList<>();
        for (ApplicationDetail applicationDetail : list) {
            com.woniu.inlet.vo.ApplicationDetail applicationDetail1 = new com.woniu.inlet.vo.ApplicationDetail();
            BeanUtils.copyProperties(applicationDetail,applicationDetail1);
            applicationDetails.add(applicationDetail1);
        }
        materialApplication.setApplicationDetails(applicationDetails);
        workingProcedures.addSystemLog("通过请领编码"+applicationCode+"获取请领的物料");
        return materialApplication;
    }

    public void receive (String s) {
        materialApplicationMapper.receive(s);
        workingProcedures.addSystemLog("请领编码为"+s+"的以领取");
    }

    public void ready (String s) {
        materialApplicationMapper.ready(s);
        workingProcedures.addSystemLog("请领编码为"+s+"的以备齐");
    }
}
